//
//  MainTabar.swift
//  weibo-mxm
//
//  Created by 马晓敏 on 16/1/11.
//  Copyright © 2016年 马晓敏. All rights reserved.
//

import UIKit

class MainTabar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame:frame)
      addSubview(plusBtn)
        
        
    }
    //重写init系统会默认当前类的对象 ,只能通过手写代码来创建
//如果通过xib创建对象,程序会奔溃
    //表示对象是从ixb创建的
    required init?(coder aDecoder: NSCoder) {
        //默认实现了一个报错
        fatalError("init(coder:) has not been implemented")
    }
  
    
   override   func layoutSubviews() {
    super.layoutSubviews()
    
    let w = self.bounds.width/5
    let h = self.bounds.height
    let rect = CGRect(x: 0, y: 0, width: w , height: h )
    
    var  index:CGFloat = 0
    for subview in subviews {
    
        if subview .isKindOfClass(NSClassFromString("UITabBarButton")!){
          //print(subview)
          subview.frame = CGRectOffset(rect, index * w, 0)
         
//            if index==1{
//              index++
//            }
//              index++
            index += (index==1 ? 2: 1)
    }
    
    plusBtn.frame = CGRectOffset(rect, 2*w, 0)
   //  bringSubviewToFront(plusBtn)
        
    }
    
    
    
    }
    
  //懒加载获取加好按钮
    lazy var plusBtn:UIButton = {
  
        let btn = UIButton()
        
     btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState:.Normal)
     btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState:.Highlighted)
        //设置按钮的图片
     btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
     btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
       btn.sizeToFit()
        
        return btn
  
        }()
    
    
}
